package cn.richard.home.view.activity

import androidx.lifecycle.lifecycleScope
import cn.richard.base.view.BaseActivity
import cn.richard.base.constant.RouterPath
import cn.richard.home.R
import cn.richard.base.constant.EXTRA
import cn.richard.base.util.RouterHelper
import cn.richard.home.databinding.ActivityPicCategoryBinding
import cn.richard.home.view.adapter.PicCategoryAdapter
import cn.richard.home.viewmodel.PicCategoryViewModel
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = RouterPath.Home.PicCategory)
class PicCategoryActivity : BaseActivity<ActivityPicCategoryBinding>() {

    private val viewModel: PicCategoryViewModel by viewModel()

    private var categoryJob: Job? = null

    private var adapter: PicCategoryAdapter = PicCategoryAdapter()

    override fun getLayoutId(): Int = R.layout.activity_pic_category

    override fun initView() {
        mBinding.clvContent.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            RouterHelper.startPicList(adapter.data[position].id)
        }
    }

    override fun initData() {
        getCategory()
    }

    private fun getCategory() {
        categoryJob?.cancel()
        categoryJob = lifecycleScope.launch {
            adapter.setNewInstance(viewModel.getPicCategory(false, 1)?.category)
        }
    }
}